#include "StringSerializer.h"

#include "Reflection/PropertyList.h"
#include "Reflection/Property.h"

bool WH::IsStringSerializeable(const CType* InType)
{
    return InType->IsStringSerializable()
        || (InType->Properties && InType->Properties->GetStringExpressionProperty())
    ;
}

CString WH::SerializeAnyToString(CAny InAny)
{
    auto Type=InAny.GetType();
    
    if(Type->IsStringSerializable())
    {
        return Type->SerializeToString(InAny.GetPointer());
    }
    else if(Type->Properties && Type->Properties->GetStringExpressionProperty())
    {
        return AnyCast<CString>(
            Type->Properties->GetStringExpressionProperty()
                ->NonStaticProperty->GetValue(InAny.GetPointer()))
        ;
    }
    else
    {
        assert(false && "Not support type");
        return CString();
    }
}

CAny WH::DeserializeAnyFromString(const CString &InString, CType *InType)
{
    if(InType->IsStringSerializable())
    {
        void * Instance=InType->CreateInstance();
        InType->DeserializeFromString(InString,Instance);
        return CAny(InType,Instance);
    }
    else if(InType->Properties && InType->Properties->GetStringExpressionProperty())
    {
        void * Instance=InType->CreateInstance();
        InType->Properties->GetStringExpressionProperty()
            ->NonStaticProperty->SetValue(Instance,InString);

        return CAny(InType,Instance);
    }
    else
    {
        assert(false && "Not support type");
        return CAny();
    }
}

CString WH::SerializeInstanceToString(CType* InType,const void* InInstance)
{
    if(InType->IsStringSerializable())
    {
        return InType->SerializeToString(InInstance);
    }
    else if(InType->Properties && InType->Properties->GetStringExpressionProperty())
    {
        return AnyCast<CString>(
            InType->Properties->GetStringExpressionProperty()
                ->NonStaticProperty->GetValue(InInstance))
        ;
    }
    else
    {
        assert(false && "Not support type");
        return CString();
    }
}
 
void WH::DeserializeInstanceFromString(const CString &InString, CType *InType, void *OutInstance)
{
    if(InType->IsStringSerializable())
    {
        InType->DeserializeFromString(InString,OutInstance);
    }
    else if(InType->Properties && InType->Properties->GetStringExpressionProperty())
    {
        InType->Properties->GetStringExpressionProperty()
            ->NonStaticProperty->SetValue(OutInstance,InString);
    }
    else
    {
        assert(false && "Not support type");
    }
}
